package com.ghy.web.backend;

import com.ghy.entity.EbOrder;
import com.ghy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 将数据放到 model中，将页面放到view中
     * spring mvc---将model渲染到view中
     * <p>
     * list ---分页请求数据
     */
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page", defaultValue = "1") int psize,
                       @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        System.out.println("start service.....;psize==>" + psize);
        //1.请求service层获取新闻的分页数据
        List<EbOrder> orderList = orderService.listByPage(psize, pageSize);
        //2.调用service层获取新闻表的总页数
        int totalPage = orderService.getMaxPage(pageSize);
        //2.传递model(数据)和view视图(jsp)
        model.addAttribute("orderList", orderList);
        //3.2总页数
        model.addAttribute("totalPage", totalPage);
        //3.3当前页数
        model.addAttribute("curPage", psize);
        return "manage/order";//视图  通过manage/news.jsp进行展示数据
    }



    @RequestMapping("/to_edit")
    public String toEdit(Model model, @RequestParam("eoId") int eoId) {
        System.out.println("点击修改跳过来了,eoId=" + eoId);
        //1.调用service层的方法查询点击的新闻的所有信息
        EbOrder ebOrder = orderService.selectOne(eoId);
        //2.指定model和jsp
        model.addAttribute("ebOrder", ebOrder);
        return "manage/order-modify";
    }
    @RequestMapping("/edit")
    public String edit(EbOrder ebOrder){
        System.out.println("ebOrder==>"+ebOrder);
        Integer result = orderService.updateOrder(ebOrder);
        if (result<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete")
    public String delete(@RequestParam ("eoId") Integer eoId) {
        //1.参数校验
        //2，执行删除（真实项目中 delete from 表名 where id=?)
        int result = orderService.remove(eoId);
        if (result ==0) {
            return "manage/manage-error";
            //TODO 要么return错误页面 要么跑出异常
        }
        return "manage/manage-result";
    }
    @RequestMapping("/selectBy")
    public String  selectBy(Model model, Integer eoId,String eoUserName,@RequestParam(value = "page", defaultValue = "1") int page,
                            @RequestParam(value = "pageSize", defaultValue = "3") int pageSize)
    {
        if ((eoId==null)&&(eoUserName.trim().length()==0))
        {
            return "manage/manage-error";
        }
        else if(eoId==null&&eoUserName.trim().length()!=0)
        {
            List<EbOrder> orderList = orderService.listByNamePage(page,pageSize,eoUserName);
            model.addAttribute("orderList", orderList);
            System.out.println(orderList);
            return "manage/order";
        }
        else if(eoId!=null)
        {
            List<EbOrder> orderList = new ArrayList<>();
            orderList.add(orderService.selectOne(eoId));
            System.out.println(orderList);
            model.addAttribute("orderList", orderList);
            return "manage/order";
        }


        return "manage/manage-error";

    }
}





